package com.sktlab.bizconfmobile.model;

import org.apache.mina.core.session.IoSession;

import android.app.Activity;

import com.sktlab.bizconfmobile.interfaces.IConfControl;
import com.sktlab.bizconfmobile.interfaces.ILoadingDialogCallback;
import com.sktlab.bizconfmobile.mina.MinaUtil;
import com.sktlab.bizconfmobile.model.manager.ConferenceManager;

public class ConfControl implements IConfControl {

	public static final String TAG = "ConfControl";

	private ConferenceManager mConfManager;

	private Conference mActiveConference;

	public static final Object obj = new Object();

	private static class InstanceHolder {

		private final static ConfControl instance = new ConfControl();
	}

	private ConfControl() {

		mActiveConference = new Conference();
		mConfManager = new ConferenceManager(mActiveConference);
	}

	public static ConfControl getInstance() {

		return InstanceHolder.instance;
	}

	@Override
	public void startConf(Activity activity, ILoadingDialogCallback callback) {

		mConfManager.startConf(activity, callback);
	}

	@Override
	public void hfControl() {
		
	}

	@Override
	public void selfMute() {

	}

	@Override
	public void allMute(int muteState, int muteParticipant) {
		
		mConfManager.allMute(muteState, muteParticipant);
	}

	@Override
	public void rollCall() {
		
		mConfManager.rollCall();
	}

	/**
	 * 
	 * @param session
	 * @param state 0 stop 1 start
	 */
	@Override
	public void record(int state) {
	
		mConfManager.record(state);
	}

	@Override
	public void lockConf(int state) {
		
		mConfManager.lockConf(state);
	}

	@Override
	public void otherFunc() {
		// TODO Auto-generated method stub

	}
	
	/**
	 * add party to current active conference which start by this app
	 * @param party
	 * @param isModerator
	 */
	public void addPartyToConf(Participant party, boolean isModerator){
		
		mConfManager.addPartyToConf(party, isModerator);
	}
	
	//add a party to the specified conference
	public void addPartyToConf(Participant party, boolean isModerator, String confId){
		
		mConfManager.addPartyToConf(party, isModerator, confId);
	}
	
	public void setServerSession(IoSession session) {
		
		mConfManager.getServerLSSession().setSession(session);
	}
	
	public IoSession getServerSession() {
		
		return mConfManager.getServerLSSession().getSession();
	}
	
	public void disconnectToServer() {
		
		mConfManager.closeSession();
	}
	
	public ConferenceManager getConfManager() {
		
		return mConfManager;
	}
	
	public Conference getActiveConference() {
		return mActiveConference;
	}

	public void setActiveConference(Conference mActiveConference) {
		this.mActiveConference = mActiveConference;
	}

	public static void notifyObj() {

		synchronized (obj) {

			obj.notifyAll();
		}
	}

	public static void waitObj() {

		synchronized (obj) {

			try {
				obj.wait(MinaUtil.CONNECT_WAITING_TIME);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void muteParty(Participant party,  int playMessage) {
	
		mConfManager.muteParty(party, playMessage);
	}
}
